//
//  RegexKitLiteViewController.m
//  GRJTolCayFrwk
//
//  Created by 易上云 on 2017/3/13.
//  Copyright © 2017年 Global Barter. All rights reserved.
//

#import "RegexKitLiteViewController.h"
#import "RegexKitLite.h"

#import "NSString+GRJString.h"


@interface RegexKitLiteViewController ()

@end

@implementation RegexKitLiteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"RegexKitLite";
    
    [self setUpButton];
}

-(void)setUpButton{
    NSArray *array = @[@"样式一",@"样式二",@"样式三",@"样式四",@"样式五"];
    
    for (int i=0; i<array.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * (self.view.bounds.size.width/array.count), 100, self.view.bounds.size.width/array.count, 44);
        button.tag = i+99;
        
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor grayColor]];
        [button addTarget:self action:@selector(regexKitLiteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:button];
    }

}


-(void)regexKitLiteButtonClick:(UIButton *)sender{
    if (sender.tag == 99) {
        
        NSString *email = @"guruijie@163.com";
        NSString *regex = @"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b";
        if ([email isMatchedByRegex:regex]) {
            NSLog(@"通过校验！");
        } else {
            NSLog(@"未通过校验，数据格式有误，请检查！");
        }
    }else if(sender.tag == 100) {
        
        NSString *str = @"#呵呵呵#[偷笑] http://foo.com/blah_blah #解放军#//http://foo.com/blah_blah @Ring花椰菜:就#范德萨发生的#舍不得打[test] 就惯#急急急#着他吧[挖鼻屎]//@崔西狮:小拳头举起又放下了 说点啥好呢…… //@toto97:@崔西狮 蹦米咋不揍他#哈哈哈# http://foo.com/blah_blah";
        //        NSString *str = @"#呵呵呵#返回加快速度会尽#解放军#快发货看电视#好几件#发货了速度了恢复良好";
        
        // 表情的规则
        NSString *emotionPattern = @"\\[[0-9a-zA-Z\\u4e00-\\u9fa5]+\\]";
        // @的规则
        NSString *atPattern = @"@[0-9a-zA-Z\\u4e00-\\u9fa5]+";
        // #话题#的规则
        NSString *topicPattern = @"#[0-9a-zA-Z\\u4e00-\\u9fa5]+#";
        // url链接的规则
        NSString *urlPattern = @"\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/)))";
        NSString *pattern = [NSString stringWithFormat:@"%@|%@|%@|%@", emotionPattern, atPattern, topicPattern, urlPattern];
        //        NSArray *cmps = [str componentsMatchedByRegex:pattern];
        
        // 遍历所有的匹配结果
        [str enumerateStringsMatchedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
            NSLog(@"%@ %@", *capturedStrings, NSStringFromRange(*capturedRanges));
        }];
        
        NSLog(@"-------------");
        
        // 以正则表达式为分隔符
        [str enumerateStringsSeparatedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
            NSLog(@"%@ %@", *capturedStrings, NSStringFromRange(*capturedRanges));
        }];
        
    }else if(sender.tag == 101){
        
        NSString *str = @"#呵呵呵#[偷笑] http://foo.com/blah_blah #解放军#//http://foo.com/blah_blah @Ring花椰菜:就#范德萨发生的#舍不得打[test] 就惯#急急急#着他吧[挖鼻屎]//@崔西狮:小拳头举起又放下了 说点啥好呢…… //@toto97:@崔西狮 蹦米咋不揍他#哈哈哈# http://foo.com/blah_blah";
        
        /**
         1.判断字符串是否符合某个特定规则
         * 判断某个字符串是否为QQ号码\电话号码\邮箱
         
         2.截取字符串中符合某个特定规则的内容
         * 截取@"#呵呵呵#[偷笑]5345 http://foo.com/blah_blah #解放军# 58937985"的所有话题\表情\链接
         */
        
        // 1.创建正则表达式
        //        NSString *pattern = @"[a-zA-Z]{1,}";
        //        NSString *pattern = @"[a-zA-Z]+";
        
        // 表情的规则
        NSString *emotionPattern = @"\\[[0-9a-zA-Z\\u4e00-\\u9fa5]+\\]";
        
        // @的规则
        NSString *atPattern = @"@[0-9a-zA-Z\\u4e00-\\u9fa5]+";
        
        // #话题#的规则
        NSString *topicPattern = @"#[0-9a-zA-Z\\u4e00-\\u9fa5]+#";
        
        // url链接的规则
        NSString *urlPattern = @"\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/)))";
        
        // | 匹配多个条件,相当于or\或
        NSString *pattern = [NSString stringWithFormat:@"%@|%@|%@|%@", emotionPattern, atPattern, topicPattern, urlPattern];
        
        NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
        // 2.测试字符串
        NSArray *results = [regex matchesInString:str options:0 range:NSMakeRange(0, str.length)];
        
        // 3.遍历结果
        for (NSTextCheckingResult *result in results) {
            NSLog(@"%@ %@", NSStringFromRange(result.range), [str substringWithRange:result.range]);
        }
        
    }else if(sender.tag == 102){
        
        NSString *username = @"6gjkhdjkhgkjh7";
        //        NSString *pattern = @"^\\d{3}";
        //        NSString *pattern = @"\\d{3}$";
        // 1.创建正则表达式
        NSString *pattern = @"^\\d.*\\d$";
        NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
        
        // 2.测试字符串
        NSArray *results = [regex matchesInString:username options:0 range:NSMakeRange(0, username.length)];
        
        NSLog(@"%zd", results.count);
        
        /**
         使用正则表达式的步骤:
         1.创建一个正则表达式对象:定义规则
         
         2.利用正则表达式对象 来测试 相应的字符串
         */
        
        /*
        Pattern : 样式\规则
        NSString *pattern = @"ab7";
        [] : 找到内部的某一个字符即可
        NSString *pattern = @"[0123456789]";
        NSString *pattern = @"[0-9]";
        NSString *pattern = @"[a-zA-Z0-9]";
        NSString *pattern = @"[0-9][0-9]";
        NSString *pattern = @"\\d\\d\\d";
        NSString *pattern = @"\\d{2,4}";
        
        ? + *
        ? : 0个或者1个
        + : 至少1个
        * : 0个或者多个
        */
    }else if(sender.tag == 103){//普通的做法:检测全数字
        
        NSString *username = @"5347858h7";
        
        BOOL flag = YES;
        for (int i = 0; i<username.length; i++) {
            unichar c = [username characterAtIndex:i];
            if (!(c >= '0' && c <= '9')) {
                flag = NO;
                break;
            }
        }
        
        if (flag) {
            NSLog(@"用户名正确");
        } else {
            NSLog(@"里面包含了非数字");
        }
        
    }
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSString *str = @"fdwfhj79832lkjl*(^jkhk";
    NSString *pattern = @"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b";

    //①匹配：传入匹配模式pattern即可，可以获取匹配到的字符串和范围。
    [str enumerateStringsMatchedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
        
        NSRange range = *capturedRanges;
        NSLog(@"匹配-->%@ %@",*capturedStrings,NSStringFromRange(range));
        
    }];
    
    //②分割：有时候有一种需求，需要获取到所有匹配到的内容之外的字串，也就是利用正则内容分割字符串，可以得到匹配到的字串以外的字符串和范围。
    [str enumerateStringsSeparatedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
        
        NSRange range = *capturedRanges;
        NSLog(@"分割-->%@ %@",*capturedStrings,NSStringFromRange(range));
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
